//
//  YYECourseViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2017/12/27.
//  Copyright © 2017年 胡波. All rights reserved.
//

#import "YYECourseViewController.h"
#import "YYECourseClassifyTableViewCell.h"
#import "YYECourseCollectionViewCell.h"
#import "YYECourseCollectionReusableView.h"
#import "YYENaivigationController.h"
#import "UISearchBar+YYESearchBar.h"
#import "YYECourseSearchViewController.h"
#import "YYECourseLeftModel.h"
#import "YYECourseRightModel.h"
#import "RequestParams.h"

@interface YYECourseViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
    
    @property (nonatomic,strong) YYEBaseTableView *tableView;
    @property (nonatomic,strong) UICollectionView *collectionView;
    @property (nonatomic,strong) NSMutableArray *leftArr;
    @property (nonatomic,strong) NSMutableArray *rightArr;
    
    @end

@implementation YYECourseViewController
    
    static NSString *leftCell = @"leftCell";
    static NSString *courseCell = @"courseCell";
    static NSString *courseReusableCell = @"reusableCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"课程分类";
    
    [self configUI];
    
    [self loadData];
    
}
    
- (void)loadData {
    
    [AFNHelper post:GetLeftList parameters:nil success:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] isEqual:@1]) {
            self.leftArr = [YYECourseLeftModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [self.tableView reloadData];
        }
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        
    }];
    
}
    
- (void)configUI {
    
    //    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 33, 30)];
    //
    //    UIImageView *searchIV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 6, 18, 18)];
    //    searchIV.image = [UIImage imageNamed:@"search"];
    //    [contentView addSubview:searchIV];
    //
    //    UITextField *textF = [[UITextField alloc]initWithFrame:CGRectMake(15, 7, SCREEN_WIDTH - 30, 30)];
    //    textF.placeholder = @"请输入搜索关键词";
    //    textF.backgroundColor = [UIColor whiteColor];
    //    textF.layer.cornerRadius = 15;
    //    textF.delegate = self;
    //    textF.leftView = contentView;
    //    textF.leftViewMode = UITextFieldViewModeAlways;
    //    textF.tintColor = [UIColor clearColor];
    //    [textF addTarget:self action:@selector(tapClicked) forControlEvents:UIControlEventTouchDown];
    //    self.navigationItem.titleView = textF;
    
    [self.view addSubview:self.tableView];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = GeneralBackColor;
    [self.tableView registerClass:[YYECourseClassifyTableViewCell class] forCellReuseIdentifier:leftCell];
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left);
        make.top.equalTo(self.view.top);
//        make.bottom.equalTo(self.view.bottom).offset(-49);
        make.bottom.equalTo(self.view.bottom);
        make.width.equalTo(77);
    }];
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.minimumLineSpacing = 0;
    flowlayout.minimumInteritemSpacing = 0;
    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowlayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[YYECourseCollectionViewCell class] forCellWithReuseIdentifier:courseCell];
    [self.collectionView registerClass:[YYECourseCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:courseReusableCell];
    
    [self.collectionView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tableView.right).offset(6);
        make.right.equalTo(self.view.right).offset(-6);
        make.top.equalTo(self.view.top);
//        make.bottom.equalTo(self.view.bottom).offset(-49);
        make.bottom.equalTo(self.view.bottom);
    }];
    
}
    
- (YYEBaseTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[YYEBaseTableView alloc]initwithDelegate:self];
    }
    return _tableView;
    
}
    
- (NSMutableArray *)leftArr {
    
    if (!_leftArr) {
        _leftArr = [NSMutableArray array];
    }
    return _leftArr;
    
}
    
- (NSMutableArray *)rightArr {
    
    if (!_rightArr) {
        _rightArr = [NSMutableArray array];
    }
    return _rightArr;
    
}
    
#pragma mark -- UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.leftArr.count;
    
}
    
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YYECourseClassifyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:leftCell];
    
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.leftArr[indexPath.row];
    if (self.leftArr.count > 0) {
        [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    }
    
    return cell;
    
}
    
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 45;
    
}
    
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //    YYECourseSearchViewController *searchVC = [[YYECourseSearchViewController alloc]init];
    //    [self.navigationController pushViewController:searchVC animated:YES];
    
    YYECourseLeftModel *model = self.leftArr[indexPath.row];
    NSDictionary *dic = @{@"id":model.id};
    [AFNHelper post:GetRightList parameters:dic success:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] isEqual:@1]) {
            self.rightArr = [YYECourseRightModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            
            for (NSInteger i = 0; i < self.rightArr.count; i++) {
                
                YYECourseLeftModel *leftmodel = [[YYECourseLeftModel alloc]init];
                leftmodel.CateName = @"全部";
                leftmodel.id = [self.rightArr[i] id];
                
                YYECourseRightModel *rightmodel = self.rightArr[i];
                if (!rightmodel.children) {
                    rightmodel.children = [NSMutableArray array];
                }
                [rightmodel.children insertObject:leftmodel atIndex:0];
                
            }
            
            [self.collectionView reloadData];
        }
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        
    }];
    
}
    
#pragma mark -- UICollectionview
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return self.rightArr.count;
    
}
    
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return [self.rightArr[section] children].count;
    
}
    
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    YYECourseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:courseCell forIndexPath:indexPath];
    
    cell.model = [self.rightArr[indexPath.section] children][indexPath.row];
    
    return cell;
    
}
    
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((SCREEN_WIDTH - 77 - 12) / 2, 50);
    
}
    
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    YYECourseCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:courseReusableCell forIndexPath:indexPath];
    
    YYECourseRightModel *model = self.rightArr[indexPath.section];
    reusableView.nameLabel.text = model.CateName;
    
    return reusableView;
    
}
    
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(SCREEN_WIDTH - 77, 30);
    
}
    
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    YYECourseCollectionViewCell *cell = (YYECourseCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell.nameLabel setBackgroundColor:[UIColor colorWithHexString:@"#efefef"]];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [cell.nameLabel setBackgroundColor:[UIColor whiteColor]];
        
        YYECourseLeftModel *model = [self.rightArr[indexPath.section] children][indexPath.row];
        
        YYECourseSearchViewController *searVC = [[YYECourseSearchViewController alloc]init];
        searVC.cid = model.id;
        [self.navigationController pushViewController:searVC animated:YES];
    });
    
    
    
}
    
    ////选择了某个cell
    //- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    //{
    //    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    //    [cell setBackgroundColor:[UIColor greenColor]];
    //}
    ////取消选择了某个cell
    //- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
    //{
    //    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    //    [cell setBackgroundColor:[UIColor redColor]];
    //}
    
    
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
    
    /*
     #pragma mark - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     // Get the new view controller using [segue destinationViewController].
     // Pass the selected object to the new view controller.
     }
     */
    
    @end

